<template>
  <transition name="in-out">
    <div v-show="flag" class="food-wrapper" ref="foodBox">
      <div class="clearfix">
      <div class="img-box">
        <img :src="food.image" alt="">
        <div class="return">
          <i class="icon-arrow_lift" @click="hide"></i>
        </div>
      </div>
      <div class="name">
        {{ food.name }}
      </div>
      <div class="desc">
        <div class="sell-count">月售{{food.sellCount}}份</div>
        <div class="rating">好评率{{food.rating}}%</div>
      </div>
      <div class="price">
        <div class="now-price"><span class="i-doller">￥</span>{{food.price}}</div>
        <div class="old-price" v-if="food.oldPrice"><span class="i-doller">￥</span>{{food.oldPrice}}</div>
        <div class="shopcar">
          <transition-group name="cart">
            <div class="btn-addcar" v-if="!foodCount || foodCount==0" @click="addFirst" key="1">加入购物车</div>
            <div class="count-wrapper" v-show="foodCount>0" key="2">
              <v-fcount :foodsinfo="food" ref="ffcount" @cart-add="addCart"></v-fcount>
            </div>
          </transition-group>
        </div>
      </div>
      <v-split></v-split>
      <div v-if="food.description">
        <div class="goods-desc">
          <div class="title">商品介绍</div>
          <div class="content">{{food.description}}</div>
        </div>
        <v-split></v-split>
      </div>
      <div class="rangting-wrapper">
        <div class="title">商品评价</div>
        <v-rangtingselect :desc="ratingsGroup" :rateNum="rateNum" :selectType="selectType"
                         :onlyContent="onlyContent" @setRateType="setRateType" @setOnlyContent="setOnlyContent"></v-rangtingselect>
        <ul v-show="ratings && ratings.length" class="rangting-ul">
          <li class="cell" v-for="item in ratings" v-show="needShow(item.rateType, item.text)" :key="item.id">
            <div class="timer">{{item.rateTime | formatDate}}</div>
            <i class="sign" :class="item.rateType===0 ? 'icon-thumb_up': 'icon-thumb_down'"></i>
            <span class="content">{{item.text}}</span>
            <div class="user">
            <span class="username">{{item.username}}</span>
            <img class="avatar" :src="item.avatar">
            </div>
          </li>
        </ul>
      </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import BScroll from 'better-scroll'
  import FoodCounter from '../foodCounter/FoodCounter'
  import Split from '../split/Split'
  import RangtingSelect from '../ratingselect/RangtingSelect'
  import {formatDate} from '../common/js/date.js'
  const ALL = 2;
  const POSITIVE = 0;
  const NEGATIVE = 1;
  export default {
    name: "food",
    props: {
      food: {
        type: Object
      }
    },
    components:{
      'v-fcount': FoodCounter,
      'v-split': Split,
      'v-rangtingselect': RangtingSelect
    },
    filters:{
      formatDate(time){
        let timer = new Date(time);
        return formatDate(timer, 'YYYY-MM-DD hh:mm');
      }
    },
    data() {
      return {
        flag: false,
        ratingsGroup:{
          all: '全部',
          positive: '推荐',
          negative: '吐槽'
      },
        onlyContent: false,
        selectType: POSITIVE,
        ratings: []
      }
    },
    watch:{
      food(val){
        this.ratings = this.food.ratings;
      }
    },
    computed:{
      foodCount(){
        return this.$store.getters.getGoodsCount[this.food.name];
      },
      rateNum(){
        let o = {};
        o.all = 0;
        o.positive = 0;
        o.negative = 0;
        if(this.ratings){
          this.ratings.forEach( item =>{
            if(item.rateType===0){
              o.positive++;
            }else{
              o.negative++;
            }
          });
          o.all = o.positive + o.negative;
        }
        return o;
      }
    },
    methods: {
      show() {
        this.flag = true;
        this.selectType = POSITIVE;
        this.onlyContent = false;
        this.$nextTick( () => {
          if(!this.scroll){
            this.scroll = new BScroll(this.$refs.foodBox,{
              click: true,
              bounceTime: 200   //动画时间 默认800忒长
            });
          }else{
            this.scroll.refresh();
          }
        })
      },
      hide() {
        this.flag = false;
      },
      addCart(el){
        this.$emit('foodAddCart',el);
      },
      addFirst(el){
        this.$refs.ffcount.addNumber(el);
      },
      needShow(type,text){
        if(this.onlyContent && !text){
          return false;
        }
        if(this.selectType === ALL){
          return true;
        }else{
          return type === this.selectType;
        }
      },
      setRateType(type){
        this.$nextTick(()=>{
          this.scroll.refresh();
        });
        this.selectType = type;
      },
      setOnlyContent(onlyContent){
        this.$nextTick(()=>{
          this.scroll.refresh();
        });
        this.onlyContent = onlyContent;
      }
    }
  }
</script>

<style lang="scss" scoped>
  .food-wrapper {
    position: fixed;
    top: 0;
    bottom: 48px;
    left: 0;
    width: 100%;
    z-index: 30;
    background-color: #fff;
    .img-box {
      position: relative;
      width: 100%;
      height: 0;
      padding-top: 100%;
      img {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
      }
      .return {
        position: absolute;
        top: 10px;
        left: 0;
        color: #fff;
        font-size: 20px;
        padding: 10px;
      }
    }
    .name {
      margin-top: 18px;
      margin-left: 18px;
      font-size: 14px;
      font-weight: 700;
      color: rgb(7, 17, 27);
      line-height: 14px;
    }
    .desc {
      margin: 8px 18px;
      color: rgb(147, 153, 159);
      font-size: 10px;
      line-height: 20px;
      display: flex;
      .sell-count {
        margin-right: 12px;
      }
    }
    .price {
      margin: 18px 0 18px 18px;
      position: relative;
      display: flex;
      .now-price {
        font-size: 14px;
        font-weight: 700;
        color: rgb(240, 20, 20);
        line-height: 24px;
        .i-doller {
          font-size: 10px;
        }
      }
      .old-price {
        color: rgb(147, 153, 159);
        font-size: 10px;
        text-decoration: line-through;
        line-height: 24px;
        margin-left: 12px;
        font-weight: 700;
        .i-doller {
          font-weight: normal;
        }
      }
      .shopcar {
        position: absolute;
        top: 0;
        right: 18px;
        .btn-addcar {
          width: 74px;
          height: 24px;
          border-radius: 12px;
          background-color: rgb(0, 160, 220);
          color: #fff;
          font-size: 10px;
          text-align: center;
          line-height: 24px;
          z-index: 30;
        }
        .count-wrapper{
          position: absolute;
          top: -6px;
          right: -4px;
          z-index: 10;
        }
      }
    }
    .goods-desc {
      margin: 18px;
      .title {
        font-size: 14px;
        color: rgb(7, 17, 27);
        line-height: 14px;
      }
      .content {
        padding: 6px 8px 0 5px;
        font-size: 12px;
        font-weight: 200;
        color: rgb(77, 85, 93);
        line-height: 24px;
      }
    }
    .rangting-wrapper{
      margin-top: 18px;
      .title{
        margin-left: 18px;
        padding-bottom: 6px;
        font-size: 14px;
        color: rgb(7, 17, 27);
        line-height: 14px;
      }
      .rangting-ul{
        .cell{
          margin: 16px 18px 0 18px;
          position: relative;
          padding-bottom: 16px;
          @include border-1px(rgba(7,17,27,.1));
          .timer{
            color: rgb(147,153,159);
            font-size: 12px;
            line-height: 24px;
            margin-bottom: 6px;
          }
          .sign{
            font-size: 12px;
            color: rgb(147,153,159);
            line-height: 24px;
            vertical-align: top;
            &.icon-thumb_up{
              color: rgb(0,160,220);
            }
          }
          .content{
            font-size: 12px;
            color: rgb(7,17,27);
            line-height: 16px;
          }
          .user{
            position: absolute;
            top: 0;
            right: 0;
            .avatar{
              height: 12px;
              width: 12px;
              border-radius: 50%;
            }
            .username{
              font-size: 10px;
              color: rgb(147,153,159);
              line-height: 12px;
            }
          }
        }
      }
    }
  }

  .in-out-enter,
  .in-out-leave-to {
    transform: translateX(100%);
  }
  .in-out-enter-active,
  .in-out-leave-active {
    transition: all 0.4s linear;
  }

  .cart-enter,
  .cart-leave-to {
    opacity: 0;
  }
  .cart-enter-active,
  .cart-leave-active {
    transition: all 0.4s linear;
  }
</style>
